package com.lzmpt.imchat.ui.chatactivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.util.EMLog;
import com.lzmpt.imchat.R;
import com.lzmpt.imchat.ui.BaseActivity;
import com.lzmpt.imchat.ui.ChatActivity;
import com.lzmpt.imchat.ui.VideoCallActivity;
import com.lzmpt.imchat.ui.VoiceCallActivity;
import com.lzmpt.imchat.widget.ChatVedioTypePoPu;

public class UserInfoActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "UserInfoActivity";
    private String toChatUsername; //环信用户ID
    private ChatVedioTypePoPu poPu;

    private TextView mTvUserName;
    private String currUserName;
    private TextView mChatVioce;
    private TextView mChatVedio;
    private LinearLayout mLlUserIntru;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        toChatUsername = getIntent().getStringExtra("userId");
        currUserName = EMClient.getInstance().getCurrentUser();
        initView();
        setUpView();


    }

    private void initView() {
        mTvUserName = findViewById(R.id.tv_user_name);
        mChatVioce = findViewById(R.id.tv_user_vioce);
        mChatVedio = findViewById(R.id.tv_user_vedio);
        mLlUserIntru = findViewById(R.id.ll_user_intranduce);

        if (!TextUtils.isEmpty(toChatUsername) && toChatUsername.equals(currUserName)) {
            mChatVioce.setVisibility(View.GONE);
            mChatVedio.setVisibility(View.GONE);
            mLlUserIntru.setVisibility(View.GONE);
        } else {
            mChatVioce.setVisibility(View.VISIBLE);
            mChatVedio.setVisibility(View.VISIBLE);
            mLlUserIntru.setVisibility(View.VISIBLE);
        }
    }

    private void setUpView() {
        mTvUserName.setText(toChatUsername);

        mChatVioce.setOnClickListener(this);
        mChatVedio.setOnClickListener(this);
        poPu = new ChatVedioTypePoPu(this);
        poPu.setMenu("视频通话", "语音通话", "取消");
        poPu.setPopuItemClick(new ChatVedioTypePoPu.PopuItemClick() {
            @Override
            public void onItemOclick(View view, int type) {
                switch (type) {
                    case 0:
                        poPu.dismiss();
                        break;
                    case 1:
                        startVideoCall();
                        break;
                    case 2:
                        startVoiceCall();
                        break;
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_user_vioce) {
            startActivity(new Intent(UserInfoActivity.this, ChatActivity.class).putExtra("userId", toChatUsername));
            finish();

        } else if (v.getId() == R.id.tv_user_vedio) {
//

            poPu.showAtLocation(v,
                    Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            poPu.show();
        }

    }

    /**
     * make a video call
     */
    protected void startVideoCall() {
        if (!EMClient.getInstance().isConnected())
            Toast.makeText(UserInfoActivity.this, R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
        else {
            startActivity(new Intent(UserInfoActivity.this, VideoCallActivity.class).putExtra("username", toChatUsername)
                    .putExtra("isComingCall", false));
            finish();
        }
    }

    /**
     * make a voice call
     */
    protected void startVoiceCall() {
        if (!EMClient.getInstance().isConnected()) {
            Toast.makeText(UserInfoActivity.this, R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
        } else {
            EMLog.i(TAG, "Intent to the ding-msg send activity.");
            startActivity(new Intent(UserInfoActivity.this, VoiceCallActivity.class).putExtra("username", toChatUsername)
                    .putExtra("isComingCall", false));
            finish();
        }
    }

}
